<?php

namespace App\Http\Controllers\UserCenter;

use App\Http\Models\User;
use App\Http\Models\Goods;
use App\Http\Models\Adverts;
use App\Http\Models\GoodsAudit;
use App\Http\Controllers\Controller;
use Auth;
use DB;
/**
 * 用户中心首页
 * Creater: lqr
 */
class UserIndexController extends Controller
{
	/**
	* 首页用户实时、审核、公告数据
	* @return string
	*/
	public function indexData(){
		$uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$today=date('Y-m-d',time());
		//获取当前正常在架商品数量 
		$shelfCount=Goods::where(array('userid'=>$uid,'goodsstate'=>2,'couponstate'=>0))->count();
		//获取今日提交的商品数量 
		$todayCreate=Goods::where('userid',$uid)->whereDate('created_at',$today)->count();
		//获取今日被拒的商品数量 
		$todayRefuse=Goods::where('userid',$uid)->where('goodsstate',3)->whereDate('created_at',$today)->count();
		//获取当前正在审核的商品数量 
		$waitAudit=Goods::where(array('userid'=>$uid,'goodsstate'=>'0'))->count();
		$datas=array(
		'shelfCount'=>$shelfCount,
		'todayCreate'=>$todayCreate,
		'todayRefuse'=>$todayRefuse,
		'waitAudit'=>$waitAudit
		);
		
		$datas1=GoodsAudit::select('gid','title','state','goodstype','content','created_at')->where('user_id',$uid)->orderBy('id','desc')->limit(10)->get();
		$datas2=Adverts::select('id','title','created_at')->orderBy('created_at','desc')->limit(10)->get();
		
		$data=array(
		'realdata'=>$datas,
		'auditdata'=>$datas1,
		'advertdata'=>$datas2
		);
		
		return json_encode(['code'=>0,'msg'=>'ok','data'=>$data]);
	}
	/**
	* 用户实时数据
	* @return string
	*/
	public function realData(){
        $uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$today=date('Y-m-d',time());
		//获取当前正常在架商品数量 
		$shelfCount=Goods::where(array('userid'=>$uid,'goodsstate'=>2,'couponstate'=>0))->count();
		//获取今日提交的商品数量 
		$todayCreate=Goods::where('userid',$uid)->whereDate('created_at',$today)->count();
		//获取今日被拒的商品数量 
		$todayRefuse=Goods::where('userid',$uid)->where('goodsstate',3)->whereDate('created_at',$today)->count();
		//获取当前正在审核的商品数量 
		$waitAudit=Goods::where(array('userid'=>$uid,'goodsstate'=>'0'))->count();
		$datas=array();
		$datas['shelfCount']=$shelfCount;
		$datas['todayCreate']=$todayCreate;
		$datas['todayRefuse']=$todayRefuse;
		$datas['waitAudit']=$waitAudit;
		return json_encode(['code'=>0,'msg'=>'ok','data'=>$datas]);
	}
	/**
	* 查询用户审核动态信息
	* @return string
	*/
	public function auditinfo(){
        $uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$size=$this->request->size;//数据个数
		if(empty($size)){
			$size=10;
		}
		$datas=GoodsAudit::where(array('user_id'=>$uid))->orderBy('id','desc')->limit($size)->get();
		return json_encode(['code'=>0,'msg'=>'ok','data'=>$datas]);
	}
	/**
	* 获取网站公告
	* @return string
	*/
	public function webadv(){
        $uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$size=$this->request->size;//数据个数
		if(empty($size)){
			$size=10;
		}
		$datas=Adverts::select('id','title','created_at')->orderBy('created_at','desc')->limit($size)->get();
		return json_encode(['code'=>0,'msg'=>'ok','data'=>$datas]);
	}
	/**
	* 获取网站公告详情
	* @return string
	*/
	public function advinfo(){
        $uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$id=$this->request->id;
		if(empty($id)){
			return json_encode(['code'=>1,'msg'=>'非法参数传递']);
		}
		$datas=Adverts::select('id','title','content','created_at')->where('id',$id)->get();
		return json_encode(['code'=>0,'msg'=>'ok','data'=>$datas]);
	}
}
